//
//  CYSelCategaryView.m
//  cyydt
//
//  Created by chu on 2019/1/29.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYSelCategaryView.h"

@implementation CYSelCategaryView


- (void)awakeFromNib{
    [super awakeFromNib];
    self.doneBtn.layer.cornerRadius = self.doneBtn.frame.size.height / 2;
    self.doneBtn.layer.masksToBounds = YES;
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"CYSelCategaryCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CYSelCategaryCollectionViewCell"];

}

- (void)setDataSourceArr:(NSArray *)dataSourceArr{
    _dataSourceArr = dataSourceArr;
//    [self initData];
    [self.collectionView reloadData];
}

- (void)initData{
    for (CYCategrayModel *cateModel in self.dataSourceArr) {
        if ([cateModel.isSelected boolValue]) {
            _selBigCateModel = cateModel;
            
            NSArray *child = cateModel.child;
            for (CYCategrayModel *smallModel in child) {
                if ([smallModel.isSelected boolValue]) {
                    _selSmallCateModel = smallModel;
                }else{
                    smallModel.isSelected = [NSNumber numberWithBool:NO];
                }
            }
        }
        
    }
}

- (void)setSinglOrMore:(BOOL)singlOrMore{
    _singlOrMore = singlOrMore;
}

- (void)setHaveSmallCategary:(BOOL)haveSmallCategary{
    _haveSmallCategary = haveSmallCategary;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    if (self.haveSmallCategary && _selBigCateModel) {
        return 2;
    }
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.haveSmallCategary && _selBigCateModel) {
        if (section == 0) {
            return 1;
        }else{
            NSArray *child = _selBigCateModel.child;
            return child.count;
        }
    }else{
        return self.dataSourceArr.count;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CYSelCategaryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CYSelCategaryCollectionViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[CYSelCategaryCollectionViewCell alloc] init];
    }
    [cell.titleBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];

    if (self.haveSmallCategary && _selBigCateModel) {
        if (indexPath.section == 0) {
            cell.model = _selBigCateModel;
            [cell.titleBtn setImage:[UIImage imageNamed:@"zpgl_fb_sy"] forState:UIControlStateNormal];
            cell.block = ^{
                self->_selBigCateModel = nil;
                self->_selSmallCateModel = nil;
                [self cleanAllSelSmallCategary];

            };
        }else{
            NSArray *child = _selBigCateModel.child;
            CYCategrayModel *smallModel = child[indexPath.item];
            cell.model = smallModel;
            cell.block = ^{
                [self cleanSmallCategaryWithModel:smallModel];
            };
        }
    }else{
        CYCategrayModel *bigModel = self.dataSourceArr[indexPath.item];
        cell.model = bigModel;
        cell.block = ^{
            //如果是单选
            if (!self.singlOrMore) {
                //单选
                [self cleanHaveSelModel:bigModel];
            }else{
                [self initDuoXuanModel:bigModel];
            }
        };
    }
    return cell;
}

#pragma mark - 多选数据的处理
- (void)initDuoXuanModel:(CYCategrayModel *)model{
    for (CYCategrayModel *cateModel in self.dataSourceArr) {
        if (cateModel == model) {
            if ([cateModel.isSelected boolValue]) {
                cateModel.isSelected = [NSNumber numberWithBool:NO];
            }else{
                cateModel.isSelected = [NSNumber numberWithBool:YES];
            }
        }
    }
    [self.collectionView reloadData];
}

- (void)cleanHaveSelModel:(CYCategrayModel *)model{
    for (CYCategrayModel *cateModel in self.dataSourceArr) {
        if (cateModel == model) {
            if ([cateModel.isSelected boolValue]) {
                _selBigCateModel = cateModel;
                break;
            }else{
                cateModel.isSelected = [NSNumber numberWithBool:YES];
                _selBigCateModel = cateModel;
            }
        }else{
            cateModel.isSelected = [NSNumber numberWithBool:NO];
        }
    }
    [self.collectionView reloadData];
}

//把选中的小分类
- (void)cleanSmallCategaryWithModel:(CYCategrayModel *)model{
    for (CYCategrayModel *cateModel in _selBigCateModel.child) {
        if (cateModel == model) {
            if ([cateModel.isSelected boolValue]) {
                cateModel.isSelected = [NSNumber numberWithBool:NO];
            }else{
                cateModel.isSelected = [NSNumber numberWithBool:YES];
                _selSmallCateModel = cateModel;
            }
        }else{
            cateModel.isSelected = [NSNumber numberWithBool:NO];
        }
    }
    [self.collectionView reloadData];
}

- (void)cleanAllSelSmallCategary{
    for (CYCategrayModel *cateModel in self.dataSourceArr) {
        NSArray *child = cateModel.child;
        for (CYCategrayModel *smallModel in child) {
            smallModel.isSelected = [NSNumber numberWithBool:NO];
        }
    }
    [self.collectionView reloadData];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((kWindowW - 71) / 3, 30);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    
    return CGSizeZero;
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (self.haveSmallCategary && _selBigCateModel && section == 1) {
        return UIEdgeInsetsMake(0, 20, 30, 20);
    }
    return UIEdgeInsetsMake(30, 20, 30, 20);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 15;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 15;
}


- (IBAction)closeAction:(UIButton *)sender {
    [GKCover hide];
}

- (IBAction)doneAction:(UIButton *)sender {
    [self ergodicAllData];
    if (self.haveSmallCategary) {
        if (!_selSmallCateModel) {
            [[UIApplication sharedApplication].keyWindow makeToast:@"请选择小分类" duration:1.5 position:CSToastPositionCenter];
        }else{
            if (self.block) {
                self.block(_selBigCateModel, _selSmallCateModel);
            }
        }
    }else{
        if (self.singlOrMore) {
            //多选
            NSMutableArray *muArr = [NSMutableArray array];
            for (CYCategrayModel *cateModel in self.dataSourceArr) {
                if ([cateModel.isSelected boolValue]) {
                    [muArr addObject:cateModel];
                }
            }
            if (self.duoxuanBlock) {
                self.duoxuanBlock(muArr);
            }
        }else{
            if (self.block) {
                self.block(_selBigCateModel, _selSmallCateModel);
            }
        }
        
    }
    
    [GKCover hide];
}

- (void)ergodicAllData{
    for (CYCategrayModel *cateModel in self.dataSourceArr) {
        if ([cateModel.isSelected boolValue]) {
            _selBigCateModel = cateModel;
            NSArray *child = cateModel.child;
            for (CYCategrayModel *smallModel in child) {
                if ([smallModel.isSelected boolValue]) {
                    _selSmallCateModel = smallModel;
                }
            }
        }else{
            NSArray *child = cateModel.child;
            for (CYCategrayModel *smallModel in child) {
                smallModel.isSelected = [NSNumber numberWithBool:NO];
            }
        }
        
    }
}

@end
